package person.wend.mybatisexample;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import person.wend.mybatisexample.plugin.achieve.Interceptor1;
import person.wend.mybatisexample.plugin.realize.InterceptorChain;
import person.wend.mybatisexample.plugin.target.DefaultTestComponent;
import person.wend.mybatisexample.plugin.target.TestComponent;

import java.lang.reflect.InvocationTargetException;

@SpringBootApplication
public class MyBatisExampleApplication {

    public static void main(String[] args) {
        // MyBatis插件机制测试
        try {
            PluginTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 插件机制测试
     */
    private static void PluginTest()
            throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        InterceptorChain interceptorChain = new InterceptorChain();

        // 通过classLoader 实现动态加载拦截器
//        String interceptor = "person.wend.mybatisexample.achieve.Interceptor1";
//        Interceptor interceptorInstance = (Interceptor) ClassUtil.resolveClass(interceptor).getDeclaredConstructor().newInstance();
//        interceptorChain.addInterceptor(interceptorInstance);
        // 简单的实现，直接添加拦截器
        interceptorChain.addInterceptor(new Interceptor1());

        TestComponent proxyComponent = (TestComponent) interceptorChain.pluginAll(new DefaultTestComponent());

        proxyComponent.test("it's a test message");
    }


}
